package service.impl.v2;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.ConfigVOMapper;
import dao.v2.LogTicketVOMapper;
import dao.v2.OrderVOMapper;
import model.LogTicketVO;
import model.MemberVO;
import service.v2.LogTicketService;
@Service
public class LogTicketServiceImpl implements LogTicketService {
	@Autowired
	private LogTicketVOMapper dao;
	@Autowired
	private OrderVOMapper orderdao;
	@Autowired
	private ConfigVOMapper ConfigDao;


	public List<LogTicketVO> selectTicketLogList(LogTicketVO logTicketVO) {
		
		String ticket_name = ConfigDao.loadConfig("TICKET_NAME");
		
		List<LogTicketVO> ticketLogList=dao.selectTicketLogList(logTicketVO);
		if(ticketLogList.size()>0){
			for(int i=0; i<ticketLogList.size();i++){
				//收入
				if("1".equals(ticketLogList.get(i).getType())||"5".equals(ticketLogList.get(i).getType())||"7".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setNum("+"+ticketLogList.get(i).getNum());
				}
				//支出
				if("2".equals(ticketLogList.get(i).getType())||"3".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setNum("-"+ticketLogList.get(i).getNum());
				}
				if("1".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setType_name(ticket_name+"充值");
				}
				if("2".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setType_name("购物");
				}
				if("3".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setType_name(ticket_name+"售出");
				}
				if("4".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setType_name("过期流转");
				}
				if("5".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setType_name(ticket_name+"售出失败退款");
				}
				if("7".equals(ticketLogList.get(i).getType())){
					ticketLogList.get(i).setType_name("系统赠送");
				}
				if(!"".equals(ticketLogList.get(i).getOrder_id())){
					String order_no = orderdao.selectOrderNo(ticketLogList.get(i).getOrder_id());
					ticketLogList.get(i).setOrder_no(order_no);
				}else{
					ticketLogList.get(i).setOrder_no("");
				}
			}
		}
		return ticketLogList;
	}

	public int getTotal(LogTicketVO logTicketVO) {
		return dao.getTotal(logTicketVO);
	}

	public LogTicketVO selectOneLogTicket(String logticket_id, String member_id) {
		
		String ticket_name = ConfigDao.loadConfig("TICKET_NAME");
		LogTicketVO oneLogTicket =new LogTicketVO();
		
		int count = dao.getOneLogTicketCount(logticket_id,member_id);
		int count1 = dao.getOneLogTicketCount1(logticket_id,member_id);
		int count2 = dao.getOneLogTicketCount2(logticket_id,member_id);
		int count3 = dao.getOneLogTicketCount3(logticket_id,member_id);
		
		if(count>=2){
			oneLogTicket = dao.selectSumLogTicketOneList(logticket_id,member_id);
		}else if(count1>=2){
			oneLogTicket = dao.selectSumLogTicketOneList1(logticket_id,member_id);
		}else if(count2>=2){
			oneLogTicket = dao.selectSumLogTicketOneList2(logticket_id,member_id);
		}else if(count3>=2){
			oneLogTicket = dao.selectSumLogTicketOneList3(logticket_id,member_id);
		}else{
			oneLogTicket = dao.selectOneLogTicket(logticket_id,member_id);
		}
		if("3".equals(oneLogTicket.getType())&&"1".equals(oneLogTicket.getBill_type())&&"3".equals(oneLogTicket.getState())){
			LogTicketVO vo =dao.getConn_no(member_id,oneLogTicket.getOrder_id());
			oneLogTicket.setConn_no(vo.getNo());
			oneLogTicket.setPage_conn_id(vo.getId());
		}
		if(oneLogTicket.getOrder_id()!=null){
			String order_no = orderdao.selectOrderNo(oneLogTicket.getOrder_id());
			oneLogTicket.setOrder_no(order_no);
		}
		if("1".equals(oneLogTicket.getType())){
			oneLogTicket.setType_name(ticket_name+"充值");
			if("2".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("充值成功");
			}
		}
		if("2".equals(oneLogTicket.getType())){
			oneLogTicket.setType_name("商城购物");
			if("2".equals(oneLogTicket.getState()) || "3".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("支付成功");
			}
		}
		if("3".equals(oneLogTicket.getType())){
			oneLogTicket.setType_name(ticket_name+"售出");
			if("1".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("待结算");
			}
			if("2".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("已结算");
			}
			if("3".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("已取消");
			}
		}
		if("4".equals(oneLogTicket.getType())){
			oneLogTicket.setType_name("过期流转");
		}
		if("5".equals(oneLogTicket.getType())){
			oneLogTicket.setType_name("退款");
			if("2".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("已退款");
			}
		}
		if("7".equals(oneLogTicket.getType())){
			oneLogTicket.setType_name("系统赠送");
			if("2".equals(oneLogTicket.getState()) || "3".equals(oneLogTicket.getState())){
				oneLogTicket.setState_name("充值成功");
			}
		}
		return oneLogTicket;
	}

	public int seleckCheckResult(String logticket_id) {
		return dao.seleckCheckResult(logticket_id);
	}

	@Override
	public LogTicketVO packPaginationInfo(LogTicketVO logTicketVO) {
		LogTicketVO pagination = new LogTicketVO();
		pagination.setPage_index(logTicketVO.getPage_index());
		pagination.setPage_size(logTicketVO.getPage_size());
		pagination.setPage_count(logTicketVO.getPage_count());
		pagination.setTotal(logTicketVO.getTotal());
		return pagination;
	}

	@Override
	public int getTotalByType1(LogTicketVO logTicketVO) {
		return dao.getTotalByType1(logTicketVO);
	}

	@Override
	public List<LogTicketVO> getListByType1(LogTicketVO logTicketVO) {
		return dao.getListByType1(logTicketVO);
	}

	


}
